let fileResolve = function () {};
let xlsxPlug;
const colNameMapKey = {
  0: "a",
  1: "b",
  2: "c",
  3: "d",
  4: "e",
  5: "f",
  6: "g",
  7: "h",
  8: "i",
  9: "j",
  10: "k",
  11: "l",
  12: "m",
  13: "n",
  14: "0",
};
document
  .getElementById("excelFileInput")
  .addEventListener("change", async function () {
    let file = this.files[0];
    if (file) {
      const fileBuffer = await file.arrayBuffer();
      const workbook = xlsxPlug.read(fileBuffer, { type: "array" });
      const sheet = workbook.Sheets[workbook.SheetNames[0]];
      const range = xlsxPlug.utils.decode_range(sheet["!ref"]);
      let output = [];
      for (let R = range.s.r; R <= range.e.r; R++) {
        let rowData = {};
        for (let L = range.s.c; L <= range.e.c; L++) {
          rowData[colNameMapKey[L]] =
            sheet[xlsxPlug.utils.encode_cell({ r: R, c: L })]?.v || "";
        }
        output.push(rowData);
      }
      xlsxPlug = null;
      fileResolve(output);
    }
  });
window.analyzExcel = (XLSX) => {
  xlsxPlug = XLSX;
  document.getElementById("excelFileInput").click();
  return new Promise((resolve) => {
    fileResolve = resolve;
  });
};
